
package ihm_clarifiee.Presentation;

import ihm_clarifiee.Control.CProjet;
import ihm_clarifiee.Control.CRelease;
import ihm_clarifiee.Control.CSprint;
import ihm_clarifiee.Control.CBacklog;
import ihm_clarifiee.Control.CUsine;
import ihm_clarifiee.application.Projet;
import ihm_clarifiee.application.Sprint;
import ihm_clarifiee.sauvegarde.Sauvegarde;
import ihm_clarifiee.test.newProjet;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import sun.awt.VerticalBagLayout;

/**
 *
 * @author 28001673
 */
public class PProjet extends javax.swing.JFrame {

    private String view;
    private JPanel viewpanel;
    private PBacklog fb;
    private PRelease prel;
    private static CProjet controle;        
    private PListeReleases ListeRelease;
    private PListeEquipe ListeEquipe;

    
  public PProjet(CProjet c) {
//	   System.out.println("PProjet.java");
	  System.out.println("Création de la présentation du projet de titre : "+c.getTitre());
	  controle =c; 
//	  fb = ((CBacklog)(controle.getBacklog())).getPresentation();
	  ListeRelease = new PListeReleases(controle);
//	  ListeRelease.setListe(ListeRelease);
	  ListeRelease.setVisible(true);
	  
	  ListeEquipe = new PListeEquipe(controle);
	  ListeEquipe.setVisible(true);

	  
	  view = "backlog";     
       
//           prel = ((CRelease)(controle.getReleaseActive())).getPresentation();
           
	  viewpanel = new JPanel();
	  viewpanel.setLayout(new FlowLayout());
	  viewpanel.setVisible(true);             
       
	  initComponents();
//	  fb.setVisible(true);
	  FlowLayout f = new FlowLayout();
       
//      viewBacklog();
      viewRelease();
      viewEquipe();
       
 /*     javax.swing.event.ChangeListener cl = new javax.swing.event.ChangeListener() {

      public void stateChanged(javax.swing.event.ChangeEvent e) {
          changeView();
      }
   };
  */    
  }
  public void setPBacklog(PBacklog pb){
	  this.fb = pb;
	  fb.setVisible(true);
	  viewBacklog();
  }
   
    private void mi_quitterActionPerformed(java.awt.event.ActionEvent evt) {                                           
    	this.dispose();
    }                                          

    
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PProjet(controle).setVisible(true);


            }
        });
    }
    
        private void initComponents() {
        	
        this.setTitle(controle.getTitre());
        setResizable(false);
        Frame_ouvrir = new javax.swing.JFileChooser();
        jSlider1 = new javax.swing.JSlider();
        jFrame1 = new javax.swing.JFrame();
        jSeparator4 = new javax.swing.JSeparator();
        panel1 = new java.awt.Panel();
        jMenuBar1 = new javax.swing.JMenuBar();
        m_fichier = new javax.swing.JMenu();
        mi_nouveau = new javax.swing.JMenuItem();
        mi_ouvrir = new javax.swing.JMenuItem();
        mi_enregistrer = new javax.swing.JMenuItem();
        mi_enregistrer_s = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mi_imp_u = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        mi_quitter = new javax.swing.JMenuItem();
//        m_edition = new javax.swing.JMenu();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel2.setLayout(new VerticalBagLayout());
        jPanel3 = new javax.swing.JPanel();
        jPanel3.setLayout(new VerticalBagLayout());
        jPanel4 = new javax.swing.JPanel();
        jPanel4.setLayout(new VerticalBagLayout());
        jPanel5= new javax.swing.JPanel();
        jPanel5.setLayout(new VerticalBagLayout());

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 500, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        jSeparator4.setOrientation(javax.swing.SwingConstants.VERTICAL);

        javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
        panel1.setLayout(panel1Layout);
        panel1Layout.setHorizontalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                
            .addGap(0, 932, Short.MAX_VALUE)
                
        );
        panel1Layout.setVerticalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           
            .addGap(0, 557, Short.MAX_VALUE)
                
                
        );

        m_fichier.setText("Fichier");

        mi_nouveau.setText("Nouveau");
        mi_nouveau.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		mi_nouveauActionPerformed(e);
        	}
        });
        m_fichier.add(mi_nouveau);

        mi_ouvrir.setText("Ouvrir");
        mi_ouvrir.addActionListener(new ActionListener() {
        	
        	public void actionPerformed(ActionEvent e) {
        		mi_ouvrirActionPerformed(e);
        	}
        });
        m_fichier.add(mi_ouvrir);    

        mi_enregistrer.setText("Enregistrer");
        m_fichier.add(mi_enregistrer);
        mi_enregistrer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mi_enregistrerActionPerformed(e);
            }
        });

        mi_enregistrer_s.setText("Enregistrer sous...");
        m_fichier.add(mi_enregistrer_s);
        m_fichier.add(jSeparator1);

        mi_imp_u.setText("Importer utilisateur");
        m_fichier.add(mi_imp_u);
        m_fichier.add(jSeparator2);

        mi_quitter.setText("Quitter");
        mi_quitter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mi_quitterActionPerformed(evt);
            }
        });
        m_fichier.add(mi_quitter);
        
        jTabbedPane1.addTab("Backlog", jPanel2);
        jTabbedPane1.addTab("Release", jPanel3);
        jTabbedPane1.addTab("Sprint", jPanel4);
        jTabbedPane1.addTab("Equipe", jPanel5);
        

        jMenuBar1.add(m_fichier);

//        m_edition.setText("Edition");
//        jMenuBar1.add(m_edition);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                //.addGap(77, 77, 77)
        //        .addComponent(jSeparator4, 20, 20,20)
                .addGap(26, 26, 26)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
               
       
       ));
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        //   .addComponent(jSeparator4, 398, 398, 398)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)           
        ));

        pack();
    }

    public void updateListeReleases()
    {
        ListeRelease.updateliste();
    }
    public void updateListeEquipe()
    {
        ListeEquipe.updateliste();
    }

    private void changeView(){
        /*if(view=="backlog"){
            view = "release";
            viewRelease();
            
        }
        
        else if (view=="release"){
            view = "backlog" ;
            viewBacklog();
        }
      */
    }

    private void mi_nouveauActionPerformed(ActionEvent e){
    	System.out.println("HERE");
    	JFrame jf = new JFrame();
    	newProjet np = new newProjet (jf, this);
    	jf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    	jf.setVisible(true);
    	jf.add(np);
    	jf.setSize(950,500);
    }
    
    private void mi_enregistrerActionPerformed(ActionEvent e){
        Sauvegarde s = new Sauvegarde(controle);
        s.save();
        String nom_fichier = controle.getNomFichier(); //� changer ds l IHM
        s.saveToXML(nom_fichier);
    }

    private void mi_ouvrirActionPerformed(ActionEvent e){
    	        javax.swing.JFileChooser jfc = new javax.swing.JFileChooser();
    	    	jfc.setVisible(true);
    	    	
    	    	File file=null;
    	    	
    	    	if (jfc.showOpenDialog(null) == jfc.APPROVE_OPTION){
    	    		file = jfc.getSelectedFile();
    	    	}
    	    	
    	    	String filename = file.getName();
    	    	try{
            		CProjet cProj = Projet.restoreFromXML(file.getPath(),file.getParent());
            		PProjet pProj = cProj.getPresentation();
            		
//            		TODO
            		CBacklog cb = cProj.getCBacklog(); 	
            		PBacklog pb = cb.getPresentation();     		
            		
            		cProj.setBacklog(cb);
//            		set la présentation du bklg au projet
            		cProj.getPresentation().setPBacklog(pb);
            		
            		System.out.println("Accueil, taille liste des histoires : "+cb.getListeHistoires().size());
            		
//            		Fenetre de presentation du projet
            		pProj.setVisible(true);
            		pProj.setSize(1500,750);
            		
//            		Restauration des champs
//            		Release
            		pProj.updateListeReleases();
            		pProj.viewRelease();
            		
            		// equipe
            		pProj.updateListeEquipe();
            		pProj.viewEquipe();
            		
            		
            		
//            		backlog
            		pb.updateAffichage();
            		pProj.viewBacklog();
            		
            		pProj.update();
            		
        	    	this.dispose();
    	    	}
    	    	catch(Exception ex){
    	    		JOptionPane.showMessageDialog(this, "Ce fichier ne permet pas de restaurer un projet.");
    	    	}  	   
    }
    
    
    public void viewBacklog(){
        jPanel2.add(fb);
        jPanel2.setName("Backlog");
    }
        
    public void viewRelease(){
        jPanel3.add(ListeRelease);
        jPanel3.setName("Release");
    }

      private void viewSprint(JPanel panel){
        jPanel4.add(panel);
        jPanel4.setName("Sprint");
    }
      private void viewEquipe(){
          jPanel5.add(ListeEquipe);
          jPanel5.setName("Equipe");
      }
        
    public PListeReleases getListeRelease()
    {
    	return ListeRelease;
    }
    public PListeEquipe getListeEquipe(){
    	return ListeEquipe;
    }
    
    public void update(){
        jPanel4.removeAll();
        if(controle.getReleaseActive()!=null)
        viewSprint(((CRelease)controle.getReleaseActive()).getPresentation());
    	//fb.updateAffichage();
    //	ListeRelease.updateAffichage();
//    	prel.updateAffichage();
    }
    
    
    
   /* public void update1(){
        jPanel4.removeAll();
        if(controle.getReleaseActive()!=null)
        viewSprint(((CRelease)controle.getReleaseActive()).getPresentation());
    	//fb.updateAffichage();
    //	ListeRelease.updateAffichage();
//    	prel.updateAffichage();
    }
    */
    
    // Variables declaration - do not modify                     
    private javax.swing.JFileChooser Frame_ouvrir;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSlider jSlider1;
//    private javax.swing.JMenu m_edition;
    private javax.swing.JMenu m_fichier;
    private javax.swing.JMenuItem mi_enregistrer;
    private javax.swing.JMenuItem mi_enregistrer_s;
    private javax.swing.JMenuItem mi_imp_u;
    private javax.swing.JMenuItem mi_nouveau;
    private javax.swing.JMenuItem mi_ouvrir;
    private javax.swing.JMenuItem mi_quitter;
    private java.awt.Panel panel1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    // End of variables declaration                   

}
